﻿Imports VPager.Settings

Public Class EditConfigWindow

    Private mPropsGrid As Grid
    Private mLabels As Hashtable
    Private mBtnSave As Button
    Private mBtnCancel As Button
    Private mIsCloseCanceled As Boolean = False

    Private Sub EditConfigWindow_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
        If SettingsManager.getInstance.NeedsSaving Then
            If Not mIsCloseCanceled Then
                If (MsgBox("Okay to close without saving changes?",
                           MsgBoxStyle.OkCancel,
                           "Cancel Setting Changes?") = MsgBoxResult.Cancel) Then
                    e.Cancel = True
                    mIsCloseCanceled = True
                    Return
                End If
            Else
                e.Cancel = True
                mIsCloseCanceled = False
                Return
            End If
        End If

        SettingsManager.getInstance.cancelChanges()
        mPropsGrid.Children.Clear()

    End Sub

    Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        mPropsGrid = FindName("gridProperties")
        mBtnSave = FindName("btnSave")
        mBtnCancel = FindName("btnCancel")
        mLabels = New Hashtable
        loadProperties()

    End Sub


    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub loadProperties()
        Dim settingsMgr = SettingsManager.getInstance
        Dim numProps = settingsMgr.Count
        Dim en = settingsMgr.getEnumerator
        Dim rowIdx = 0

        While en.MoveNext
            setRowDefinition()
            setRowBackground(rowIdx, Windows.Media.Brushes.LightGray)
            setRowContent(rowIdx, en.Current)
            rowIdx += 1
        End While

        ''Add  an incative row at end to show where current config file is located.
        'setRowDefinition()
        'setRowBackground(rowIdx, Windows.Media.Brushes.LightGray)
        'Dim settingLabel = New Label()
        'settingLabel.Content = "Settings Store Location"
        ''settingLabel.IsEnabled = False
        'Grid.SetRow(settingLabel, rowIdx)
        'Grid.SetColumn(settingLabel, 0)
        'mPropsGrid.Children.Add(settingLabel)
        'Dim settingUIElem = New TextBox()
        'settingUIElem.Text = settingsMgr.ConfigFile
        'settingUIElem.IsEnabled = False
        'settingUIElem.Margin = New Thickness(3)
        'Grid.SetRow(settingUIElem, rowIdx)
        'Grid.SetColumn(settingUIElem, 1)
        'mPropsGrid.Children.Add(settingUIElem)


    End Sub

    Private Sub setRowDefinition()
        Dim rowDef = New RowDefinition()
        rowDef.MinHeight = 32
        rowDef.MaxHeight = 32
        mPropsGrid.RowDefinitions.Add(rowDef)

    End Sub

    Private Sub setRowContent(ByVal rowIdx As Integer, ByRef setting As BaseSetting)
        AddHandler setting.ValueChanged, AddressOf settingChangedHandler
        Dim settingLabel = New Label()
        settingLabel.Content = setting.Name
        settingLabel.ToolTip = setting.Description
        mLabels.Add(setting.ID, settingLabel)

        'Debug.Write("Adding Test Prop: " & setting.Name & "  Row: " & rowIdx & vbNewLine)
        Grid.SetRow(settingLabel, rowIdx)
        Grid.SetColumn(settingLabel, 0)
        mPropsGrid.Children.Add(settingLabel)
        Dim settingUIElem = setting.UIElement
        settingUIElem.Margin = New Thickness(3)
        Grid.SetRow(settingUIElem, rowIdx)
        Grid.SetColumn(settingUIElem, 1)
        mPropsGrid.Children.Add(settingUIElem)

    End Sub

    Private Sub setRowBackground(ByVal rowIdx As Integer, ByVal brush As Windows.Media.Brush)
        Dim background = New Rectangle()
        background.Height = 30
        background.Fill = brush
        Grid.SetRow(background, rowIdx)
        Grid.SetColumn(background, 0)
        Grid.SetColumnSpan(background, 2)
        mPropsGrid.Children.Add(background)

    End Sub

    Private Sub settingChangedHandler(ByVal sender As BaseSetting, ByVal e As SettingEventArgs)
        Dim lbl As Label = mLabels(e.Setting.ID)
        If Not IsNothing(lbl) Then
            lbl.FontWeight = FontWeights.Bold
            mBtnCancel.Content = "Cancel"
            mBtnSave.IsEnabled = True
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click
        SettingsManager.getInstance.save()
        Me.Close()
    End Sub

    Private Sub btnHidden_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnHidden.Click
        MsgBox("Configuration Store Location:" & vbNewLine &
               SettingsManager.getInstance.ConfigFile,
               MsgBoxStyle.OkOnly,
               "Config Location")
    End Sub
End Class
